<?php

/**
 * Description of StatusDAO
 *
 * @author Hiago
 */
class StatusDAO {

    public function inserir(Status $status) {
        $con = new bd();
        try {
            $con->beginTransaction();
            $stmt = $con->prepare("INSERT INTO `status` (`idStatus`,`descricao`) VALUES (?,?)");

            $dados = Array($status->getIdStatus(), $status->getDescricao());
            $stmt->execute($dados);
            $id = $con->lastInsertId();
            $con->ocorreramErros($stmt);
            $status->setIdStatus($id);
            $con->commit();
        } catch (Exception $ex) {
            $con->rollBack();
        }
    }

    public static function getById($idStatus) {
        $con = new bd();
        $stmt = $con->prepare("SELECT `status`.`idStatus`, `status`.`descricao`
                               FROM `status` WHERE `status`.`idStatus` = ?;");
        $stmt->execute(Array($idStatus));
        $con->ocorreramErros($stmt);

        $r = $stmt->fetch();
        return new Status($r[0], $r[1]);
    }

    public function listar() {
        $con = new bd();
        $stmt = $con->prepare("SELECT `status`.`idStatus`, `status`.`descricao`
                               FROM `status`");
        $stmt->execute();
        $con->ocorreramErros($stmt);

        $statusArray = Array();
        while ($r = $stmt->fetch()) {
            $statusArray[] = new Status($r[0], $r[1]);
        }
        return $statusArray;
    }

}

?>
